<?php

namespace App\Models\CacheModel\wecaht;

use App\Models\Api\BaseCache;
use think\Log;
use AstroTool\Cache\CacheManage;

class WechatLingqianUserCards extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.wechat_lingqian_user_cards';

    protected $cacheModel = 'wechat_h5.user_cards';

    /**
     * 获取用户 卡牌 信息
     */
    public function getUserCardsInfo(int $uid)
    {
        $resData = CacheManage::getInstance()->setCacheConfig($this->cacheModel, $uid)
            ->getDataAndSet(function () use ($uid)
            {
                try
                {
                    $data = $this->where(['uid' => $uid])->column('id,num', 'card_id');
                    $resData = [];
                    if ($data)
                    {
                        foreach ($data as $cardId => $info)
                        {
                            $resData[(string)$cardId] = json_encode($info);
                        }
                    }

                    return $resData;
                }
                catch (\Exception $e)
                {
                    return [];
                }
            });

        foreach ($resData as $cardId => $textInfoItem)
        {
            $resData[$cardId] = json_decode($textInfoItem, true);
        }

        return $resData;
    }

    /**
     * 用户 卡牌 变动
     * @param int $uid
     * @param array $changeData [ id  num  card_id change_num=>变动数量]
     * @param int $type 类型 1=>投币抽牌 2=>兑换月运免费券 3=>兑换年运免费券
     * @return bool
     */
    public function userChangeCardsInfo(int $uid, array $changeData, int $type)
    {
        try
        {
            $time = time();
            $resChacheData = [];
            $changeRecord = [];
            foreach ($changeData as $changeItem)
            {
                if (isset($changeItem['id']))
                {
                    $this->where('id', $changeItem['id'])->update([
                        'num' => $changeItem['num'],
                        'update_time' => $time,
                    ]);

                } else
                {
                    $id = $this->insertGetId([
                        'uid' => $uid,
                        'card_id' => $changeItem['card_id'],
                        'num' => $changeItem['num'],
                        'create_time' => $time,
                    ]);
                    $changeItem['id'] = $id;
                }

                $resChacheData[$changeItem['card_id']] = json_encode([
                    'id' => $changeItem['id'],
                    'card_id' => $changeItem['card_id'],
                    'num' => $changeItem['num'],
                ]);

                $changeRecord[] = [
                    'uid' => $uid,
                    'card_id' => $changeItem['card_id'],
                    'change_num' => $changeItem['change_num'],
                    'type' => $type,
                    'create_time' => $time,
                ];
            }
            // 更新 用户拥有 卡牌 缓存
            CacheManage::getInstance()->setCacheConfig($this->cacheModel, $uid)->setCache($resChacheData);

            (new WechatLingqianUserCardsChangeRecode())->saveAll($changeRecord);
        }
        catch (\Exception $e)
        {
            Log::write("微信公众号 灵签活动 字更新失败：{$e->getMessage()};File:{$e->getFile()};Line:{$e->getLine()}", 'error', true);
            return false;
        }

        return true;
    }
}
